home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / TUTOROOT.PAK / STEP07.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  6KB  |  284 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1994 by Borland International
  3. //   Tutorial application -- step07.cpp
  4. //----------------------------------------------------------------------------
  5. #include <owl/pch.h>
  6. #include <owl/applicat.h>
  7. #include <owl/framewin.h>
  8. #include <owl/dc.h>
  9. #include <owl/inputdia.h>
  10. #include <owl/opensave.h>
  11. #include <classlib/arrays.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "step07.rc"
  15.  
  16. typedef TArray<TPoint> TPoints;
  17. typedef TArrayIterator<TPoint> TPointsIterator;
  18.  
  19. class TDrawWindow : public TWindow {
  20.   public:
  21.     TDrawWindow(TWindow* parent = 0);
  22.    ~TDrawWindow()
  23.     {
  24.       delete DragDC;
  25.       delete Pen;
  26.       delete Line;
  27.       delete FileData;
  28.     }
  29.  
  30.     void SetPenSize(int newSize);
  31.  
  32.   protected:
  33.     TDC* DragDC;
  34.     int PenSize;
  35.     TPen* Pen;
  36.     TPoints* Line; // To store points in line.
  37.     TOpenSaveDialog::TData* FileData;
  38.     bool IsDirty, IsNewFile;
  39.  
  40.     // Override member function of TWindow
  41.     bool CanClose();
  42.  
  43.     // Message response functions
  44.     void EvLButtonDown(uint, TPoint&);
  45.     void EvRButtonDown(uint, TPoint&);
  46.     void EvMouseMove(uint, TPoint&);
  47.     void EvLButtonUp(uint, TPoint&);
  48.     void Paint(TDC&, bool, TRect&);
  49.     void CmFileNew();
  50.     void CmFileOpen();
  51.     void CmFileSave();
  52.     void CmFileSaveAs();
  53.     void CmAbout();
  54.     void SaveFile();
  55.     void OpenFile();
  56.  
  57.   DECLARE_RESPONSE_TABLE(TDrawWindow);
  58. };
  59.  
  60. DEFINE_RESPONSE_TABLE1(TDrawWindow, TWindow)
  61.   EV_WM_LBUTTONDOWN,
  62.   EV_WM_RBUTTONDOWN,
  63.   EV_WM_MOUSEMOVE,
  64.   EV_WM_LBUTTONUP,
  65.   EV_COMMAND(CM_FILENEW, CmFileNew),
  66.   EV_COMMAND(CM_FILEOPEN, CmFileOpen),
  67.   EV_COMMAND(CM_FILESAVE, CmFileSave),
  68.   EV_COMMAND(CM_FILESAVEAS, CmFileSaveAs),
  69.   EV_COMMAND(CM_ABOUT, CmAbout),
  70. END_RESPONSE_TABLE;
  71.  
  72. TDrawWindow::TDrawWindow(TWindow* parent)
  73. {
  74.   Init(parent, 0, 0);
  75.   DragDC    = 0;
  76.   PenSize   = 1;
  77.   Pen       = new TPen(TColor::Black, PenSize);
  78.   Line      = new TPoints(10, 0, 10);
  79.   IsNewFile = true;
  80.   IsDirty   = false;
  81.   FileData  = new TOpenSaveDialog::TData(OFN_HIDEREADONLY|OFN_FILEMUSTEXIST,
  82.                                         "Point Files (*.PT7)|*.pt7|", 0, "",
  83.                                         "PT7");
  84. }
  85.  
  86. bool
  87. TDrawWindow::CanClose()
  88. {
  89.   if (IsDirty)
  90.     switch(MessageBox("Do you want to save?", "Drawing has changed",
  91.                       MB_YESNOCANCEL | MB_ICONQUESTION)) {
  92.       case IDCANCEL:
  93.         // Choosing Cancel means to abort the close -- return false.
  94.         return false;
  95.  
  96.       case IDYES:
  97.         // Choosing Yes means to save the drawing.
  98.         CmFileSave();
  99.     }
  100.   return true;
  101. }
  102.  
  103. void
  104. TDrawWindow::EvLButtonDown(uint, TPoint& point)
  105. {
  106.   Line->Flush();
  107.   Invalidate();
  108.  
  109.   if (!DragDC) {
  110.     SetCapture();
  111.     DragDC = new TClientDC(*this);
  112.     DragDC->SelectObject(*Pen);
  113.     DragDC->MoveTo(point);
  114.     Line->Add(point);
  115.     IsDirty = true;
  116.   }
  117. }
  118.  
  119. void
  120. TDrawWindow::EvRButtonDown(uint, TPoint&)
  121. {
  122.   char inputText[6];
  123.  
  124.   wsprintf(inputText, "%d", PenSize);
  125.   if ((TInputDialog(this, "Line Thickness",
  126.                         "Input a new thickness:",
  127.                         inputText,
  128.                         sizeof(inputText))).Execute() == IDOK) {
  129.     int newPenSize = atoi(inputText);
  130.  
  131.     if (newPenSize < 0)
  132.       newPenSize = 1;
  133.  
  134.     SetPenSize(newPenSize);
  135.   }
  136. }
  137.  
  138. void
  139. TDrawWindow::EvMouseMove(uint, TPoint& point)
  140. {
  141.   if (DragDC) {
  142.     DragDC->LineTo(point);
  143.     Line->Add(point);
  144.   }
  145. }
  146.  
  147. void
  148. TDrawWindow::EvLButtonUp(uint, TPoint&)
  149. {
  150.   if (DragDC) {
  151.     ReleaseCapture();
  152.     delete DragDC;
  153.     DragDC = 0;
  154.   }
  155. }
  156.  
  157. void
  158. TDrawWindow::SetPenSize(int newSize)
  159. {
  160.   delete Pen;
  161.   PenSize = newSize;
  162.   Pen     = new TPen(TColor::Black, PenSize);
  163. }
  164.  
  165. void
  166. TDrawWindow::Paint(TDC& dc, bool, TRect&)
  167. {
  168.   bool first = true;
  169.   TPointsIterator i(*Line);
  170.  
  171.   dc.SelectObject(*Pen);
  172.  
  173.   while (i) {
  174.     TPoint p = i++;
  175.  
  176.     if (!first)
  177.       dc.LineTo(p);
  178.     else {
  179.       dc.MoveTo(p);
  180.       first = false;
  181.     }
  182.   }
  183.   dc.RestorePen();
  184. }
  185.  
  186. void
  187. TDrawWindow::CmFileNew()
  188. {
  189.   if (CanClose()) {
  190.     Line->Flush();
  191.     Invalidate();
  192.     IsDirty = false;
  193.     IsNewFile = true;
  194.   }
  195. }
  196.  
  197. void
  198. TDrawWindow::CmFileOpen()
  199. {
  200.   if (CanClose())
  201.     if ((TFileOpenDialog(this, *FileData)).Execute() == IDOK)
  202.       OpenFile();
  203. }
  204.  
  205. void
  206. TDrawWindow::CmFileSave()
  207. {
  208.   if (IsNewFile)
  209.     CmFileSaveAs();
  210.   else
  211.     SaveFile();
  212. }
  213.  
  214. void
  215. TDrawWindow::CmFileSaveAs()
  216. {
  217.   if (IsNewFile)
  218.     strcpy(FileData->FileName, "");
  219.  
  220.   if ((TFileSaveDialog(this, *FileData)).Execute() == IDOK)
  221.     SaveFile();
  222. }
  223.  
  224. void
  225. TDrawWindow::CmAbout()
  226. {
  227.   TDialog(this, IDD_ABOUT).Execute();
  228. }
  229.  
  230. void
  231. TDrawWindow::SaveFile()
  232. {
  233.   ofstream os(FileData->FileName);
  234.  
  235.   if (!os)
  236.     MessageBox("Unable to open file", "File Error", MB_OK | MB_ICONEXCLAMATION);
  237.   else {
  238.     os << Line->GetItemsInContainer();
  239.     TPointsIterator  i(*Line);
  240.     while (i)
  241.       os << i++;
  242.     IsNewFile = IsDirty = false;
  243.   }
  244. }
  245.  
  246. void
  247. TDrawWindow::OpenFile()
  248. {
  249.   ifstream is(FileData->FileName);
  250.  
  251.   if (!is)
  252.     MessageBox("Unable to open file", "File Error", MB_OK | MB_ICONEXCLAMATION);
  253.   else {
  254.     Line->Flush();
  255.     unsigned  numPoints;
  256.     is >> numPoints;
  257.     while (numPoints--) {
  258.       TPoint point;
  259.       is >> point;
  260.       Line->Add(point);
  261.     }
  262.   }
  263.  
  264.   IsNewFile = IsDirty = false;
  265.   Invalidate();
  266. }
  267.  
  268. class TDrawApp : public TApplication {
  269.   public:
  270.     TDrawApp() : TApplication() {}
  271.  
  272.     void InitMainWindow()
  273.     {
  274.       SetMainWindow(new TFrameWindow(0, "Drawing Pad", new TDrawWindow));
  275.       GetMainWindow()->AssignMenu("COMMANDS");
  276.     }
  277. };
  278.  
  279. int
  280. OwlMain(int /*argc*/, char* /*argv*/ [])
  281. {
  282.   return TDrawApp().Run();
  283. }
  284.